﻿using Autodesk.AutoCAD.Runtime;
using Autodesk.Windows;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.Windows;
using Autodesk.AutoCAD.Windows;
using HPDI.DrawingStandard;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;

[assembly: ExtensionApplication(typeof(HPDIConfigMaster.Main))]
[assembly: CommandClass(typeof(HPDIConfigMaster.HPDIMaster))]
namespace HPDIConfigMaster
{
    public class Main : IExtensionApplication
    {
        bool isCreate = false;
        //接口实现
        public void Initialize()
        {
            try
            {
               
                AddRibbon();//添加ribbon菜单的函数  
                Autodesk.Windows.ComponentManager.ItemInitialized += this.ComponentManager_ItemInitialized;
                isCreate = true;//利用委托添加菜单
            }
            catch (System.Exception ex)
            {
              
            }
        }
        public void Terminate()
        {
            throw new NotImplementedException();
        }
        public void ComponentManager_ItemInitialized(object sender, RibbonItemEventArgs e)
        {
            if (Autodesk.Windows.ComponentManager.Ribbon != null)
            {

                try
                {
                    if(!isCreate||string.IsNullOrWhiteSpace(CurPaht.curPaht))
                        AddRibbon();//添加ribbon菜单的函数  

                }
                catch { }

                Autodesk.Windows.ComponentManager.ItemInitialized -= this.ComponentManager_ItemInitialized;

            }
        }

        [CommandMethod("AddRibbon")]
        public void AddRibbon()
        {
            Autodesk.Windows.RibbonControl ribbonCtrl =
                Autodesk.AutoCAD.Ribbon.RibbonServices.RibbonPaletteSet.RibbonControl;
            RibbonTab tab = ribbonCtrl.AddTab("设计标准", "Acad.RibbonId1", true); //给Ribbon界面添加一个选项卡
            CurPaht.curPaht = Path.GetDirectoryName(this.GetType().Assembly.Location) + "\\"; //获取程序集的加载路径

            RibbonPanelSource panelDataSource0 = tab.AddPanel("初始化"); //给选项卡添加面板   

            panelDataSource0.Items.Add(RibbonButtonInfos.SYSSetting); //添加查询出图桥梁信息命令按钮           

            CurPaht.curPaht = Path.GetDirectoryName(this.GetType().Assembly.Location) + "\\"; //获取程序集的加载路径            
        }
       
    }
    public class HPDIMaster
    {
        [CommandMethod("sys")]
        public void InitHPDIDrawingStandard()
        {
            InitConfig.InitStandardConfig();
        }
             
        [CommandMethod("test")]
        public void TestA()
        {
            Database acCurDb = HostApplicationServices.WorkingDatabase;

            InitConfig.InitLayer();
            InitConfig.InitTextStyle("H仿宋", "仿宋", 0.7);
            InitConfig.InitDimStyle(Unit.Millimeter, Unit.Millimeter, 1, "H仿宋");
            InitConfig.InitDimStyle(Unit.Millimeter, Unit.Centimeter, 10, "H仿宋");
            InitConfig.InitDimStyle(Unit.Millimeter, Unit.Millimeter, 20, "H仿宋");
            InitConfig.InitBlock();

            Point2d p1 = new Point2d(0, 0);
            Point2d p2 = new Point2d(125, 0);
            Point2d p3 = new Point2d(125, 200);
            Point2d p4 = new Point2d(0, 210);
            Point2d p5 = new Point2d(-50, 0);

            System.Data.DataTable dt = new System.Data.DataTable("名称");//创建表
            dt.Columns.Add("ID", typeof(Int32));//添加列
            dt.Columns.Add("Name", typeof(String));
            dt.Columns.Add("Age", typeof(Int32));
            dt.Rows.Add(new object[] { 1, "张三", 20 });//添加行
            dt.Rows.Add(new object[] { 2, "李四", 25 });
            dt.Rows.Add(new object[] { 3, "王五", 30 });
            acCurDb.CreatLayout("ceshi","", false, 1);
            var objs1 = PLPloter.AddPolylineByPointList(new List<Point2d>() {p1,p2,p3,p4,p5 },"H虚线",false);
            //var objs2 = 
            //var objs3 = DimPloter.DimRot(p1.C3D(), p2.C3D(), p4.C3D(),0,20) ;
            //// var objs4 = 
            //objs1.Add(objs3);
            //objs3 = TextPloter.AddTitle(Point2d.Origin.C2D(-100, 50), "断面图", "");
            //var objs4 = DimPloter.AddBG(100, p5.C3D(),8);
            ////objs1.Add(objs2);
            //objs1.Add(objs3);
            ////objs1.Add(objs4);
            ////objs3 = DimPloter.AddArrowTitle(Point2d.Origin.C3D(-100, 50), "断面图", 2.5,9);
            ////objs1.Add(objs3);
            //objs3= DimPloter.AddCirText("P2", Point2d.Origin.C2D(-100, 50), 7, "H仿宋", 20- 90, 2.5, 3);
            //objs1.Add(objs3);
            objs1= PLPloter.AddArcPoly(p1.C2D(), 1000, 1000, "H虚线", 200);
            var objids= Ploter.WriteDatabase(objs1);


            var objids2= DimPloter.AddPMBJ("A", p1.C3D(),1,2.5,false);

            objids.Add(objids2);

            ObjectIdCollection oc = new ObjectIdCollection();
            oc.Add(objids2);
            var ext =Ploter.GetExtends(oc);

            acCurDb.AddToModelSpace(ext.ConvertPL());


        }




    }
}
